<?php

namespace app\common\model;
use app\common\library\helper;
/**
 * 规格/属性(值)模型
 * Class SpecValue
 * @package app\common\model
 */
class SpecValue extends BaseModel
{
    protected $name = 'spec_value';
    protected $updateTime = false;

    /**
     * 关联规格组表
     * @return $this|\think\model\relation\BelongsTo
     */
    public function spec()
    {
        return $this->belongsTo('Spec');
    }

    // 
    public function getSkuSpec($goodsSku)
    {
        $spec_sku_str = isset($goodsSku['spec_sku_id']) ? $goodsSku['spec_sku_id'] : ''; 
        $spec_value_ids = explode('_',$spec_sku_str);
        if (!empty($spec_value_ids)) {
            $value_list = $this->with('spec')
                        ->field('*')
                        ->where('spec_value_id','in', $spec_value_ids)
                        ->select();
            $specAttrData = [];
            foreach ($value_list as $item) {
                if (!isset($specAttrData[$item['spec_id']])) {
                    $specAttrData[$item['spec_id']] = [
                        'group_id'   => $item['spec']['spec_id'],
                        'group_name' => $item['spec']['spec_name'],
                        'spec_items' => [],
                    ];
                }
                $specAttrData[$item['spec_id']]['spec_items'][] = [
                    'item_id'    => $item['spec_value_id'],
                    'spec_value' => $item['spec_value'],
                ];
            }
            return $specAttrData;
        }
    }

}
